package com.taiji.dianthus.service.sms.impl;

import com.taiji.dianthus.common.BusinessMessage;
import com.taiji.dianthus.dao.sms.SmsGroupRepository;
import com.taiji.dianthus.domain.sms.SmsGroup;
import com.taiji.dianthus.service.sms.SmsGroupService;
import com.taiji.dianthus.service.sms.SmsGroupUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName SmsInfoServiceImnpl
 * @Description
 * @Author H.M
 * @Date 2020/7/15
 */
@Service
public class SmsGroupServiceImpl implements SmsGroupService {

    @Autowired
    private SmsGroupRepository repository;

    @Autowired
    private SmsGroupUserService groupUserService;

    /**
     * 查询日志列表
     *
     * @param pageSize
     * @param currentPage
     * @param groupName
     * @return
     */
    @Override
    public BusinessMessage list(Integer currentPage, Integer pageSize, String groupName, String userId) {
        BusinessMessage message = new BusinessMessage();
        try {
            Sort sort = new Sort(Sort.Direction.ASC, "groupOrder").and(new Sort(Sort.Direction.DESC, "createTime"));
            //分页参数
            Pageable pageable = PageRequest.of(currentPage, pageSize, sort);
            Page<SmsGroup> smsGroupPage;
            //创建查询参数
            Specification<SmsGroup> spec = (Root<SmsGroup> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {
                List<Predicate> pl = new ArrayList<Predicate>();
                if (!StringUtils.isEmpty(groupName)) {
                    pl.add(cb.like(root.<String>get("groupName"), "%" + groupName + "%"));
                }
                //查询未删除的
                pl.add(cb.equal(root.<Integer>get("enableFlag"), 1));
                pl.add(cb.equal(root.<Integer>get("createUserId"), userId));
                return cb.and(pl.toArray(new Predicate[0]));
            };
            smsGroupPage = repository.findAll(spec, pageable);
            message.setMsg("查询群组列表成功");
            message.setData(smsGroupPage);
            message.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("查询群组列表失败");
        }
        return message;
    }


    /**
     * 保存方法
     *
     * @param smsGroup
     * @return
     */
    @Override
    public BusinessMessage save(SmsGroup smsGroup) {
        BusinessMessage message = new BusinessMessage();
        try {
            repository.save(smsGroup);
            message.setSuccess(true);
            message.setMsg("保存群组信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("保存群组信息失败,系统异常");
        }
        return message;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public BusinessMessage delete(String id) {
        BusinessMessage message = new BusinessMessage();
        try {
            //判断应用下是否注册了服务
            Integer countService = groupUserService.countByGroupId(id);
            if (countService > 0) {
                message.setMsg("该群组下还有人员绑定，不能删除");
            } else {
                repository.updateFlag(id);
                message.setSuccess(true);
                message.setMsg("删除群组信息成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            message.setMsg("删除群组信息失败,系统异常");
        }
        return message;
    }

    @Override
    public SmsGroup getOneById(String id) {
        return repository.getOne(id);
    }

    @Override
    public BusinessMessage checkGroupName(String groupName, String groupId, String userId) {
        BusinessMessage message = new BusinessMessage();
        Integer countGroup = 0;
        if (groupId == null) {
            countGroup = repository.checkGroupName(groupName,userId);
        } else {
            countGroup = repository.checkEditGroupName(groupName, groupId,userId);
        }
        if (countGroup <= 0) {
            message.setSuccess(true);
        }
        return message;
    }

    @Override
    public List<SmsGroup> getAllGroupNoPage(String id) {
        return repository.getAllGroupNoPage(id);
    }


}
